package com.leo;

import com.almasb.fxgl.animation.Interpolators;
import com.almasb.fxgl.app.CursorInfo;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.input.UserAction;
import com.almasb.fxgl.physics.CollisionHandler;
import com.almasb.fxgl.physics.HitBox;
import com.leo.component.BowComponent;
import com.leo.constant.ArcheryType;
import com.leo.entity.ArcheryEntityFactory;
import javafx.scene.input.MouseButton;
import javafx.util.Duration;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-06  23:15
 * @Description: 射箭游戏
 */
public class ArcheryGameApp extends GameApplication {

    private Entity bow;

    @Override
    protected void initSettings(GameSettings settings) {
        settings.setWidth(800);
        settings.setHeight(600);
        settings.setDefaultCursor(new CursorInfo("cursorDefault.png", 0, 0));
    }

    @Override
    protected void initGame() {
        FXGL.getGameWorld().addEntityFactory(new ArcheryEntityFactory());
        FXGL.spawn("bg");
        bow = FXGL.spawn("bow", new SpawnData(0, 220));
        FXGL.spawn("target", 750, 260);
        FXGL.spawn("shootBar", 35, 3);
        FXGL.spawn("scoreShow", 1, 1 + 44 / 2);
    }

    @Override
    protected void initInput() {
        FXGL.getInput().addAction(new UserAction("shoot") {
            @Override
            protected void onAction() {
                BowComponent component = bow.getComponent(BowComponent.class);
                component.shoot();
            }

            @Override
            protected void onActionBegin() {
                FXGL.spawn("cursor", FXGL.getInput().getMousePositionUI().subtract(1120 / 7 / 2, 160 / 2));
            }
        }, MouseButton.PRIMARY);
    }

    @Override
    protected void initPhysics() {
        // 碰撞检测
        FXGL.getPhysicsWorld().addCollisionHandler(new CollisionHandler(ArcheryType.TARGET, ArcheryType.ARROW) {
            @Override
            protected void onHitBoxTrigger(Entity a, Entity b, HitBox boxA, HitBox boxB) {
                FXGL.play("hit.wav");
                // 碰撞动画
                FXGL.spawn("star", b.getRightX() - 2138 / 8 / 2, b.getY() + 1 - 276 / 2);
                // 显示分数
//                FXGL.spawn("score", new SpawnData(b.getRightX() - 11, b.getY() - 14).put("area", boxA.getName()));
                FXGL.spawnWithScale("score", new SpawnData(b.getRightX() - 11, b.getY() - 14).put("area", boxA.getName()), Duration.seconds(0.8), Interpolators.BOUNCE.EASE_OUT());
                // 碰撞以后移除箭头
                b.removeFromWorld();
                // 碰撞位置
                System.out.println(boxA.getName());
            }
        });
    }

    @Override
    protected void onUpdate(double tpf) {
//        System.out.println(FXGL.getGameWorld().getEntities().size());
    }

    public static void main(String[] args) {
        launch(args);
    }
}
